(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:updatepayment
     * @description
     * # pager 
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.updatepayment.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('updatePayment', function() {
            return {
                restrict: 'E',
                template: "<a href='javascript:void(0);' class='sui-btn btn-success' ng-click='updatepayment();'>修改价格</a>",
                scope: {
                    orderid: '@',
                    backcall: "&",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$state',
                    function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $state) {
                        $scope.updatepayment = function() {
                            $scope.orderData().then(showPop);
                        }

                        function showPop() {
                            $.alert({
                                backdrop: 'static',
                                keyboard: true,
                                title: '<span>修改订单价格</span>',
                                body: $templateCache.get("views/directives.updatepayment.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                // hasfoot: false,
                                width: '880px',
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $scope.updatePrice();
                                },
                            });
                        }

                        $scope.orderData = function(pageNo) {
                            return dataService.orderOne($scope.orderid).then(function(one) {
                                if (one) {
                                    //sku显示
                                    var items = one.items;
                                    for (var i in items) {
                                        var properties = items[i].sku_properties;
                                        var a = properties.split(";");
                                        var c = "";
                                        for (var j in a) {
                                            if (a[j]) {
                                                var b = a[j].split(":");
                                                var d = b[2] + ":" + b[3];
                                                c = d + ";" + c;
                                            }
                                        }
                                        items[i].sku_name = c;
                                        items[i].pay_discount = 0;
                                        items[i].pay_disprice = items[i].payment;
                                    }
                                    one.items = items;
                                    $scope.order = one;
                                    // console.error("order--->",$scope.order);

                                } else {
                                    helper.myAlert("No data found");

                                }
                            });
                        }

                        $scope.updatePrice = function() {
                            var items = $scope.order.items;

                            var adjust_discounts = [];
                            for (var i in items) {
                                if (!items[i].discount_fee) {
                                    items[i].discount_fee = 0;
                                }
                                var a = { "sku_id": items[i].sku_id, "discount": parseFloat(items[i].discount_fee) };
                                adjust_discounts.push(a);
                            }

                            var data = { "order_id": $scope.orderid, "adjust_discounts": { "adjust_discounts": adjust_discounts } };
                            // console.error("订单修改价格送入的数据--->", data);
                            // return false;
                            dataService.discountUpdate(data).then(function(result) {
                                // console.error("result-->", result);
                                if (result.order_id) {
                                    helper.myAlert("success");
                                    if($scope.backcall){
                                        $scope.backcall();
                                    }
                                } else {
                                    helper.myAlert("error：" + result.Msg);
                                    return false;
                                }
                            });
                        }
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.$watch('order', function(newValue, oldValue, scope) {
                        if (newValue) {
                            var items = scope.order.items;
                            var a = 0;
                            for (var i in items) {
                                var price = items[i].discount_fee;
                                if (!price) {
                                    price = 0;
                                }
                                a += parseFloat(price);
                            }
                            scope.order.discount_fee = a;
                        }
                    }, true);
                }
            }
        })
})();